package handler

import (
	"bytes"
	"fmt"
	"github.com/BSNDA/PCNGateway-Go-SDK/errno"
	"github.com/BSNDA/PCNGateway-Go-SDK/model"
	"github.com/gin-gonic/gin"
	"io"
	"io/ioutil"
	"log"
)

//查下一站的节点的地址
func FetchNextAdress(c *gin.Context) {

	//直接参数解析
	buf := &bytes.Buffer{}
	tea := io.TeeReader(c.Request.Body, buf)
	body, err := ioutil.ReadAll(tea)
	if err!=nil{
		log.Fatal(err.Error())}
	data:=string(body)
	  message,err:=model.GetAdress(data)
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	fmt.Println(message)
	SendResponse(c, nil, message)

}


//物流节点数据上链操作
func UploadRouter (c *gin.Context){

	buf := &bytes.Buffer{}
	tea := io.TeeReader(c.Request.Body, buf)
	body, err := ioutil.ReadAll(tea)
	if err!=nil{
		log.Fatal(err.Error())}
	//oderinfo :=model.Decode(body)
	//c.Writer.WriteString(oderinfo.OrderId+"/  "+oderinfo.Adresses[1].Node+"/  "+oderinfo.Adresses[1].NextAdr)
	     data:=string(body)
	     fmt.Println(body)
	     //上链函数
	message,err:=model.SetAdress(data)
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}

	if (message=="SUCCESS"){
		var suc string="物流节点数据上链成功！"
		SendResponse(c, nil, suc)} else {
		var faild string="上链失败！"
		SendResponse(c, nil, faild)}
	}




func FetchBCMessageTest(c *gin.Context) {

	message,err:=model.Get1()
	if err != nil {
		SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	fmt.Println(message)
	SendResponse(c, nil, message)

}